﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MAPIdotnet;

namespace OutlookHub.Models
{
    public class Store : IDisposable
    {
        private IMAPIMsgStore store;
        private Dictionary<byte[], Folder> folders = new Dictionary<byte[], Folder>();

        public string Name { get { return store.DisplayName; } }

        public IMAPIMsgStoreID Id { get { return store.StoreID; } }


        public Folder ReceiveFolder
        {
            get
            {
                return GetFolder(store.ReceiveFolder);
            }
        }

        public Folder SentFolder
        {
            get { return GetFolder(store.SentMailFolder); }
        }

        public Folder RootFolder
        {
            get { return GetFolder(store.RootFolder); }
        }

        public Folder TrashFolder
        {
            get { return GetFolder(store.TrashFolder); }
        }

        public Folder OutboxFolder
        {
            get { return GetFolder(store.OutboxFolder); }
        }

        public IEnumerable<Folder> Folders
        {
            get
            {
                return RootFolder.SubFolders;
            }
        }


        public Folder GetFolder(IMAPIFolderID id)
        {
            byte[] fid = id.AsByteArray;
            Folder folder;
            if (folders.TryGetValue(fid, out folder)) return folder;
            folder = new Folder(id, this);
            folders.Add(fid, folder);
            return folder;
        }

        public void Dispose()
        {
            foreach (Folder f in folders.Values)
            {
                f.Dispose();
            }
            folders.Clear();
            store.Dispose();
            GC.SuppressFinalize(this);
        }

        ~Store()
        {
            Dispose();
        }

        public int UnreadMessagesCount
        {
            get
            {
                return GetUnreadMessageCount();
            }
        }

        public Store(IMAPIMsgStore store)
            : base()
        {
            this.store = store;
          //  store.EventNotifyMask = EEventMask.ObjectCreated | EEventMask.ObjectDeleted | EEventMask.ObjectModified | EEventMask.NewMail | EEventMask.ObjectCopied;
        }


        private int GetUnreadMessageCount()
        {
            int cnt = 0;
            foreach (var f in Folders)
            {
                foreach (var msg in f.Messages)
                {
                    if (!msg.ReadFlag) cnt++;
                }
            }
            return cnt;
        }


        public event EventHandler<MessageEventArgs> MessageEvent
        {
            add { store.MessageEvent += value; }
            remove { store.MessageEvent -= value; }
        }

        public event EventHandler<NewMessageEventArgs> NewMessageEvent
        {
            add { store.NewMessageEvent += value; }
            remove { store.NewMessageEvent -= value; }
        }

        public event EventHandler<FolderEventArgs> FolderEvent
        {
            add { store.FolderEvent += value; }
            remove { store.FolderEvent -= value; }
        }

        public event EventHandler<StoreEventArgs> StoreEvent
        {
            add { store.StoreEvent += value; }
            remove { store.StoreEvent -= value; }
        }
    }
}
